# import requests
import json
import unittest
from api.login import LoginAPI
# 绝对路径
import app
# 参数化导包
from parameterized import parameterized

# 构造函数
def build_data():
    file = app.BASE_DIR + '/data/test01_login_build.json'
    test_data = []
    with open(file,encoding='utf_8') as f:
        json_data = json.load(f)
        for case_data in json_data:
            login_build_data = case_data.get('login_build_data')
            status_code = case_data.get('status_code')
            success = case_data.get('success')
            code = case_data.get('code')
            message = case_data.get('message')
            test_data.append((login_build_data,status_code,success,code,message))
    return test_data



# 定义测试类
class TestLoginAPI(unittest.TestCase):
    # 前置处理
    def setUp(self):
        # 实例化
        self.login_api = LoginAPI()

    @parameterized.expand(build_data())
    # 登陆成功
    def test001_login(self,login_build_data,status_code,success,code,message):
        # 调用登陆接口
        response = self.login_api.login(login_build_data)
        print(response.json())
        # 断言
        self.assertEqual(status_code,response.status_code)
        self.assertEqual(success,response.json().get('success'))
        self.assertEqual(code,response.json().get('code'))
        self.assertIn(message,response.json().get('message'))